package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.dto.ChannelDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by zhangjh on 2016/10/24.
 */
@Repository
public class MerchantChannelRepositoryImpl implements MerchantChannelRepositoryCustom {


    private static final Logger log = LoggerFactory.getLogger(CustomerBankRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public PageResult findByMerchantId(Long merchantId, Long mallId, String status, List<Long> types, String name, String channelClientType, PageResult pageResult) {
        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from channel c where c.is_delete='NO' and c.number is null");
            sql.append(" and c.merchant_id=:merchantId");
            sql.append(" and c.mall_id=:mallId");
            params.put("merchantId", merchantId);
            params.put("mallId", mallId);
            if (types != null && types.size()>0) {
                sql.append(" and c.channel_type_id in(:types)");
                params.put("types", types);
            }
            if (StringUtils.isNotBlank(status)) {
                if (status != null) {
                    sql.append(" and c.channel_status=:status");
                    params.put("status", status);
                }
            }
            if (StringUtils.isNotBlank(name)) {
                sql.append(" and (c.id=:key or c.name like :name)");
                params.put("name", "%" + name + "%");
                params.put("key", name);
            }
            if (StringUtils.isNotBlank(channelClientType)) {
                sql.append(" and c.channel_client_type=:channelClientType");
                params.put("channelClientType", channelClientType);
            }
            sql.append(" ORDER BY  c.date_created desc");
            fields.append("c.id,c.name,c.channel_status,c.icon_url,c.channel_type_id,c.number,c.app_flag,c.app_default,c.channel_client_type");
        };

        return doing.go(pageResult, ChannelDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public ChannelDto getChannelById(Long id, Long merchantId, Long mallId, Whether isDelete) {
        SQLObjectDoing<ChannelDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from channel c where c.is_delete=:isDelete");
            sql.append(" and c.merchant_id=:merchantId");
            sql.append(" and c.mall_id=:mallId");
            sql.append(" and c.id=:channelId");
            params.put("isDelete", isDelete.toString());
            params.put("merchantId", merchantId);
            params.put("mallId", mallId);
            params.put("channelId", id);

            fields.append("c.*");

        };

        return doing.go(ChannelDto.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<ChannelDto> getTop5Channel(Long mallId, Long merchantId) {

        SQLListDoing<ChannelDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select c.name as name ,cs1.view_count/(select sum(cs.view_count)  from channel_statistics cs  where cs.mall_id=:mallId and `merchant_id`=:merchantId)*0.6+cs1.sale_count/(select sum(cs.sale_count)  from channel_statistics cs where cs.mall_id=:mallId and `merchant_id`=:merchantId)*0.4 as star ");
            sql.append(" from channel_statistics cs1 left join  channel c on c.id=cs1.channel_id");
            sql.append(" where cs1.mall_id=:mallId and cs1.`merchant_id`=:merchantId  and c.channel_status='USING'");
            sql.append(" order by cs1.view_count/(select sum(cs.view_count) from channel_statistics cs where cs.mall_id=:mallId and `merchant_id`=:merchantId)*0.6+cs1.sale_count/(select sum(cs.sale_count) from channel_statistics cs where cs.mall_id=:mallId and `merchant_id`=:merchantId)*0.4 desc ");
            sql.append(" limit 5");
            params.put("merchantId", merchantId);
            params.put("mallId", mallId);

//            fields.append("c.name");

        };

        return doing.go(ChannelDto.class, jdbcTemplate, log, objectMapper);
    }
}
